package 所有的题类.ZA.链表;

import com.kiwi.nixu.common.LNode;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class 合并两个链表 {

    public static void main(String[] args) {
        LNode lNode1 = arrayToLNode(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
        LNode lNode2 = arrayToLNode(Arrays.asList(5, 5, 5));
        LNode lNode = mergeInBetween(lNode1, 2, 4, lNode2);
        System.out.println(lNode.data);
    }
    public static LNode arrayToLNode(List<Integer> list){
        LNode lNode = new LNode(list.get(0));
        LNode cur=lNode;
        for (int i = 1; i < list.size(); i++) {
            LNode tmp=new LNode(list.get(i));
            cur.next=tmp;
            cur=tmp;
        }
        return lNode;
    }

    public static LNode mergeInBetween(LNode list1, int a, int b, LNode list2) {
        //遍历链表list1
        int c1=1;
        LNode head=list1;
        LNode a1=null;
        LNode b1=null;
        while(list1!=null){
            if(c1==(a)){
                a1=list1;
            }
            if(c1==(b+2)){
                b1=list1;
            }
            c1++;
            list1=list1.next;
        }
        LNode bHead=list2;
        LNode bTail=null;
        while(list2!=null){
            list2=list2.next;
            if (list2!=null) {
                bTail=list2;
            }
        }
        a1.next=bHead;
        bTail.next=b1;
        return head;
    }
}
